package ams.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ams.dao.IBaseDao;
import ams.model.dto.ExecResult;
import ams.model.entity.Rights;
import ams.util.RightsType;
import ams.util.SysConstans;

@Service("rightsService")
public class RightsService implements IRightsService {

	@Autowired
	private IBaseDao<Rights> rightsDao;

	@Override
	public ExecResult add(Rights rights) {
		ExecResult execResult = new ExecResult();

		String nameIsExistHql = "select count (*) from Rights r where r.name = ? and r.pid = ?";
		String codeIsExistHql = "select count (*) from Rights r where r.code = ? and r.pid = ?";

		boolean nameIsExist = rightsDao.count(nameIsExistHql, new Object[] { rights.getName(), rights.getPid() }) != 0 ? true : false;
		boolean codeIsExist = rightsDao.count(codeIsExistHql, new Object[] { rights.getCode(), rights.getPid() }) != 0 ? true : false;

		if (nameIsExist) {
			execResult.getMessage().add("权限名称重复！");
		}
		if (codeIsExist) {
			execResult.getMessage().add("权限编码重复！");
		}

		if (nameIsExist || codeIsExist) {
			execResult.setSuccess(false);
			return execResult;
		} else {
			// 根据权限类型判断是否为叶子节点
			if (RightsType.getTypeByid(rights.getType()).isLeaf()) {
				rights.setState(SysConstans.TREEGRID_IS_LEAF);
			} else {
				rights.setState(SysConstans.TREEGRID_ISNOT_LEAF);
			}
			rightsDao.saveOrUpdate(rights);
			execResult.setSuccess(true);
			execResult.getMessage().add("添加权限成功！");
			return execResult;
		}

	}

	@Override
	public ExecResult delete(Rights rights) {
		List<String> msg = new ArrayList<>();

		String hasChildrenHql = "select count(*) from Rights r where r.pid = ?";
		boolean hasChildren = rightsDao.count(hasChildrenHql, new Object[] { rights.getId() }) != 0 ? true : false;

		if (hasChildren) {
			msg.add("该权限下包含子权限");
			return new ExecResult(false, msg);
		} else {
			String delRightsHql = "delete from Rights r where r.id = ? ";
			rightsDao.executeHql(delRightsHql, new Object[] { rights.getId() });
			msg.add("删除成功！");
			return new ExecResult(true, msg);
		}

	}

	@Override
	public ExecResult update(Rights rights) {
		ExecResult execResult = new ExecResult();

		String nameIsExistHql = "select count (*) from Rights r where r.name = ? and r.pid = ? and r.id != ?";
		String codeIsExistHql = "select count (*) from Rights r where r.code = ? and r.pid = ? and r.id != ?";

		boolean nameIsExist = rightsDao.count(nameIsExistHql,
				new Object[] { rights.getName(), rights.getPid(), rights.getId() }) != 0 ? true : false;
		boolean codeIsExist = rightsDao.count(codeIsExistHql,
				new Object[] { rights.getCode(), rights.getPid(), rights.getId() }) != 0 ? true : false;

		if (nameIsExist) {
			execResult.getMessage().add("权限名称重复！");
		}
		if (codeIsExist) {
			execResult.getMessage().add("权限编码重复！");
		}

		if (nameIsExist || codeIsExist) {
			execResult.setSuccess(false);
			return execResult;
		} else {
			// 根据权限类型判断是否为叶子节点
			if (RightsType.getTypeByid(rights.getType()).isLeaf()) {
				rights.setState(SysConstans.TREEGRID_IS_LEAF);
			} else {
				rights.setState(SysConstans.TREEGRID_ISNOT_LEAF);
			}
			rightsDao.saveOrUpdate(rights);
			execResult.setSuccess(true);
			execResult.getMessage().add("更新权限成功！");
			return execResult;
		}

	}

	@Override
	public List<Rights> getRightsByPid(Integer id) {
		List<Rights> rightsList = new ArrayList<>();
		String rootRightsHql = "from Rights r where r.pid is null";
		String rightsByPidHql = "from Rights r where r.pid = ? ";

		if (id == null) {
			rightsList = rightsDao.Find(rootRightsHql);
		} else {
			rightsList = rightsDao.Find(rightsByPidHql, new Object[] { id });
		}

		return rightsList;
	}

	@Override
	public List<Rights> getAllRights() {
		return rightsDao.Find("select new Rights(id,pid,name) from Rights");
	}

}
